package com.baichuan.zntx.controller;

import java.util.List;

import lombok.RequiredArgsConstructor;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.constraints.*;
import cn.dev33.satoken.annotation.SaCheckPermission;
import org.springframework.web.bind.annotation.*;
import org.springframework.validation.annotation.Validated;
import com.baichuan.common.idempotent.annotation.RepeatSubmit;
import com.baichuan.common.log.annotation.Log;
import com.baichuan.common.web.core.BaseController;
import com.baichuan.common.mybatis.core.page.PageQuery;
import com.baichuan.common.core.domain.R;
import com.baichuan.common.core.validate.AddGroup;
import com.baichuan.common.core.validate.EditGroup;
import com.baichuan.common.log.enums.BusinessType;
import com.baichuan.common.excel.utils.ExcelUtil;
import com.baichuan.zntx.domain.vo.CommandVo;
import com.baichuan.zntx.domain.bo.CommandBo;
import com.baichuan.zntx.service.ICommandService;
import com.baichuan.common.mybatis.core.page.TableDataInfo;

/**
 * 指令记录
 *
 * @author Baichuan
 * @date 2024-05-11
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/zntx-app/command")
public class CommandController extends BaseController {

    private final ICommandService commandService;

    /**
     * 查询指令记录列表
     */
    @SaCheckPermission("zntx-app:command:list")
    @GetMapping("/list")
    public TableDataInfo<CommandVo> list(CommandBo bo, PageQuery pageQuery) {
        return commandService.queryPageList(bo, pageQuery);
    }

    /**
     * 导出指令记录列表
     */
    @SaCheckPermission("zntx-app:command:export")
    @Log(title = "指令记录", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(CommandBo bo, HttpServletResponse response) {
        List<CommandVo> list = commandService.queryList(bo);
        ExcelUtil.exportExcel(list, "指令记录", CommandVo.class, response);
    }

    /**
     * 获取指令记录详细信息
     *
     * @param id 主键
     */
    @SaCheckPermission("zntx-app:command:query")
    @GetMapping("/{id}")
    public R<CommandVo> getInfo(@NotNull(message = "主键不能为空")
                                     @PathVariable String id) {
        return R.ok(commandService.queryById(id));
    }

    /**
     * 新增指令记录
     */
    @SaCheckPermission("zntx-app:command:add")
    @Log(title = "指令记录", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody CommandBo bo) {
        return toAjax(commandService.insertByBo(bo));
    }

    /**
     * 修改指令记录
     */
    @SaCheckPermission("zntx-app:command:edit")
    @Log(title = "指令记录", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody CommandBo bo) {
        return toAjax(commandService.updateByBo(bo));
    }

    /**
     * 删除指令记录
     *
     * @param ids 主键串
     */
    @SaCheckPermission("zntx-app:command:remove")
    @Log(title = "指令记录", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")
                          @PathVariable String[] ids) {
        return toAjax(commandService.deleteWithValidByIds(List.of(ids), true));
    }
}
